package com.pengtu.web.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.HibernateUtils;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Authority;
import com.pengtu.entity.app.Role;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 角色管理Action.
 * 
 * 演示不分页的简单管理界面.
 * 
 * @author calvin
 */
@Namespace("/account")
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "role.action", type = "redirect") })
public class RoleAction extends BaseActionSupport<Role> {

	private static final long serialVersionUID = -4052047494894591406L;
	private AccountManager accountManager;
	// -- 页面属性 --//
	private String id;
	private Role entity;
	private List<Role> allRoleList;// 角色列表
	private List<String> checkedAuthIds;// 页面中钩选的权限id列表
	private Page<Role> page = new Page<Role>(10);// 每页5条记录

	// -- ModelDriven 与 Preparable函数 --//
	public Role getModel() {
		return entity;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getRole(id);
		} else {
			entity = new Role();
		}
	}

	// -- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		QueryFilter filters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchRole(page, filters);
		//allRoleList = accountManager.getAllRole();
		return SUCCESS;
	}
	
	@Action("/account/json/role")
	public String searchForTree() throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<Role> roles = accountManager.getAllRole();
				

		for (Role  role : roles) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("leaf", Boolean.TRUE);
			map.put("text", role.getName());
			map.put("id", role.getId());
		
			list.add(map);
		}
		Struts2Utils.renderJson(list, "no-cache");
		return null;
	}

	@Override
	public String input() throws Exception {
		if (StringUtils.isEmpty(id)) {

		} else {
			checkedAuthIds = entity.getAuthIds();
		}
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// 根据页面上的checkbox 整合Role的Authorities Set.
		HibernateUtils.mergeByCheckedIds(entity.getAuthorityList(),
				checkedAuthIds, Authority.class);
		// 保存用户并放入成功信息.
		if (StringUtils.isEmpty(id)) {
			entity.setCreateDate(new Date());
			entity.setDelFlag("0");
		}
		accountManager.saveRole(entity);
		addActionMessage("保存角色成功");
		return RELOAD;
	}

	// -- 其他Action函数 --//
	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkRoleName() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newRoleName = request.getParameter("roleName");
		String oldRoleName = request.getParameter("oldRoleName");

		newRoleName = new String(newRoleName.getBytes("iso8859-1"), "utf-8");
		oldRoleName = new String(oldRoleName.getBytes("iso8859-1"), "utf-8");

		if (accountManager.isRoleNameUnique(newRoleName, oldRoleName)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		// 因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	@Override
	public String delete() throws Exception {
		accountManager.deleteRole(id);
		addActionMessage("删除角色成功");
		return RELOAD;
	}

	// -- 页面属性访问函数 --//
	/**
	 * list页面显示所有角色列表.
	 */
	public List<Role> getAllRoleList() {
		return allRoleList;
	}

	/**
	 * input页面显示所有授权列表.
	 */
	public List<Authority> getAllAuthorityList() {
		return accountManager.getAllAuthority();
	}

	/**
	 * input页面显示角色拥有的授权.
	 */
	public List<String> getCheckedAuthIds() {
		return checkedAuthIds;
	}

	/**
	 * input页面提交角色拥有的授权.
	 */
	public void setCheckedAuthIds(List<String> checkedAuthIds) {
		this.checkedAuthIds = checkedAuthIds;
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public Page<Role> getPage() {
		return page;
	}
}